package trading_strategy.strategies;

import org.apache.log4j.Logger;
import trading_strategy.Strategy;
import trading_strategy.events.*;
import trading_strategy.instruments.ListedInstrument;

import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: gderoujoux
 * Date: 24 mai 2010
 * Time: 16:26:16
 * To change this template use File | Settings | File Templates.
 */
public class SignedTradesStrategy extends Strategy {
    public static final Logger LOGGER = Logger.getLogger(SignedTradesStrategy.class);
    private SimpleDateFormat totSdf = new SimpleDateFormat("dd/MM/yyyy,HH:mm:ss.SSS");
    private Date date = this.date;

    private static final char CSV_SEPARATOR = ',';

    ListedInstrument instrument1;

    @Override
    public double getPnL() {
        return 0;
    }

    Writer writer;

    public SignedTradesStrategy(Date date, ListedInstrument instrument, Writer writer) {
        this.date = date;
        this.instrument1 = instrument;
        this.writer = writer;
    }

    @Override
    public void onMarketEvent(AbstractMarketEvent event) {
        try {
            if (event instanceof PublicTradeUpdate) {
                PublicTradeUpdate ptu = (PublicTradeUpdate) event;
                writer.write("FXCE0");
                writer.write(",");
                writer.write(totSdf.format(event.getDate()));
                writer.write(",");
                writer.write("Trade");
                writer.write(",");
                writer.write(String.valueOf(ptu.getPrice()));
                writer.write(",");
                writer.write(String.valueOf(ptu.getQty()));
                writer.write(",");
                writer.write(ptu.getSide().toString());
                writer.write("\n");
            } else if (event instanceof OrderBookUpdate) {
                OrderBookUpdate obu = (OrderBookUpdate) event;
                writer.write("FXCE0");
                writer.write(",");
                writer.write(totSdf.format(event.getDate()));
                writer.write(",");
                writer.write("Quote");
                writer.write(",");
                writer.write(",");
                writer.write(",");
                writer.write(",");
                OrderBook book = obu.getBook();
                if (book.getBidDepth() > 0) {
                    writer.write(String.valueOf(book.getBid(0)));
                    writer.write(",");
                    writer.write(String.valueOf(book.getBidQty(0)));
                    writer.write(",");
                }
                if (book.getAskDepth() > 0) {
                    writer.write(String.valueOf(book.getAsk(0)));
                    writer.write(",");
                    writer.write(String.valueOf(book.getAskQty(0)));
                    writer.write(",");
                }
                writer.write("\n");
            }
        } catch (Exception e) {

        }
    }

    public void register() {
        // Subscribe to events classes
        EventManager.getInstance().subscribe(getListener(), instrument1.getTradeEventProvider());
        EventManager.getInstance().subscribe(getListener(), instrument1.getOrderBookProvider());
    }

    @Override
    public void stop() {
        super.stop();
    }
}